package com.example.teesystem.controller;

import com.example.teesystem.common.exception.CustomException;
import com.example.teesystem.common.utils.Response;
import com.example.teesystem.common.utils.ResultCode;
import com.example.teesystem.common.valid.MapContains;
import com.example.teesystem.entity.AdvancedTrainingProgress;
import com.example.teesystem.entity.vo.advancedTrain.AdTrainProgressUploadVo;
import com.example.teesystem.service.AdvancedTrainingProgressService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/tee/advanceTrainProgress", produces = "application/json; charset=utf-8")
@RequiredArgsConstructor
@Validated
public class AdvanceTrainProgressController {
    private final AdvancedTrainingProgressService trainingProgressService;
    
    /**
     * 添加
     * @param adTrainProgressUploadVo uploadVo
     * @return
     */
    @PostMapping("/add")
    public Response add(@Valid @RequestBody AdTrainProgressUploadVo adTrainProgressUploadVo) {
        if(trainingProgressService.add(adTrainProgressUploadVo)) {
            return Response.ok();
        } else {
            throw new CustomException(ResultCode.ADD_INFO_FAIL);
        }
    }
    
    /**
     * 获取某个学生进阶训练做题情况
     * @param map
     * @return
     */
    @PostMapping("/getById")
    public Response get(
            @MapContains(keys = {"studentId"}, message = "获取参数失败")
            @RequestBody Map<String, Integer> map) {
        List<AdvancedTrainingProgress> result = trainingProgressService.get(map.get("studentId"));
        if(!result.isEmpty()) {
            return Response.ok(result);
        } else {
            throw new CustomException(ResultCode.GET_INFO_FAIL);
        }
    }
    
    /**
     * 获取多个学生答题情况
     * @param map
     * @return
     */
    @PostMapping("/getByIds")
    public Response getByIds(@RequestBody Map<String, List<Integer>> map) {
        List<Integer> studentIds = map.get("studentIds");
        if(studentIds == null || studentIds.isEmpty()) {
            throw new CustomException(ResultCode.PARAMETER_ERROR.getMsg(), ResultCode.PARAMETER_MISS.getCode());
        }
        List<AdvancedTrainingProgress> result = trainingProgressService.get(studentIds);
        if(!result.isEmpty()) {
            return Response.ok(result);
        } else {
            throw new CustomException(ResultCode.GET_INFO_FAIL);
        }
    }

    @PostMapping(value = "/getfinishAndOpenId")
    public Response getOpenAd(@RequestBody Map<String,Integer> map){
        Integer courseId = map.get("courseId");
        if(courseId == null){
            return Response.error(ResultCode.PARAMETER_ERROR);
        }
        return trainingProgressService.getFinishAndOpenId(courseId);
    }
}
